package main;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.NoninvertibleTransformException;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;


public class Clef {

	public static final char TREBLE_CLEF_BOTTOM_LINE = 'e';
	public static final char BASS_CLEF_BOTTOM_LINE = 'g';

	private Image clef;
	private boolean treble;
	private Score score;

	/**
	 * 
	 * @param s Current score
	 * @param type clef type
	 */
	public Clef(Score s, String type){
		score = s;
		createClefImage(type);
	}

	/**
	 * Generates and loads the clef image
	 * @param c clef type
	 */
	private void createClefImage(String c) {
		MediaTracker tracker = new MediaTracker(score);
		try{
			if(c.charAt(0) == 't'){
				clef = ImageIO.read(new File("treble_clef.gif"));
				treble = true;
			}else{
				clef = ImageIO.read(new File("bass_clef.gif"));
				treble = false;
			}
		}catch(IOException e){}
		tracker.addImage(clef, 0);
		tracker.checkAll();
		int s = treble? Score.TOTAL_STAFF_SIZE*3/2: Score.TOTAL_STAFF_SIZE*7/8;
		clef = clef.getScaledInstance(-1,s,Image.SCALE_SMOOTH); //scales the image
	}

	/**
	 * Draws the clefs, bar, and key signature
	 * @param quant number of lines
	 * @param g current graphics context
	 */
	public int draw(Graphics2D g, int hOff){
		//hOff is the the horizontal position of where everything will be drawing
		hOff = treble? -clef.getWidth(null)/2+Score.NOTE_HEAD_SIZE: -clef.getWidth(null)/2+Score.NOTE_HEAD_SIZE*2;
		if (treble)
			g.drawImage(clef, hOff, -Score.BAR_LINE_SPACING, null);
		else
			g.drawImage(clef, hOff,0, null);
		hOff += clef.getWidth(null)+10; //move the cursor positon over
		return hOff;
	}

	/**
	 * 
	 * @return whether or not the clef is treble
	 */
	public boolean isTreble(){
		return treble;
	}
	
	/**
	 * Returns "treble" or "bass" depending on the clef
	 */
	public String toString(){
		return treble? "treble":"bass";
	}
}
